<template>
  <div class="main">
    <div class="publish">
      <el-tabs tab-position="left" style="min-height: 520px;" :stretch="true">
        <el-tab-pane label="已发布">
          <div class="publish-list" v-if="publishes.length > 0">
            <div class="publish-list-item" v-for="publish in publishes.slice((publishPage - 1) * 5, publishPage * 5)"
              :key="publish.id" @click="goPublish(publish.id)">
              <div class="publish-list-item-cover">
                <img :src="publish.cover" alt="图片找不到了QAQ">
              </div>
              <div class="publish-list-item-text">
                <h1 class="publish-list-item-title">
                  {{ publish.title }}
                </h1>
                <div>
                  <span class="publish-list-item-description">
                    {{ publish.description }}
                  </span>
                </div>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="publishes.length" :page-size="5"
              layout="prev, pager, next" @current-change="changePublishPage">
            </el-pagination>
          </div>
          <el-empty v-else description="这里什么也没有" style="margin-top:80px;"></el-empty>
        </el-tab-pane>
        <el-tab-pane label="未发布" v-if="isSelf">
          <div class="publish-list" v-if="drafts.length > 0">
            <div class="publish-list-item" v-for="draft in drafts.slice((draftPage - 1) * 5, draftPage * 5)"
              :key="draft.id" @click="goDraft(draft.id)">
              <div class="publish-list-item-cover">
                <img :src="draft.cover" alt="图片找不到了QAQ">
              </div>
              <div class="publish-list-item-text">
                <h1 class="publish-list-item-title">
                  {{ draft.title }}
                </h1>
                <div>
                  <span class="publish-list-item-description">
                    {{ draft.description }}
                  </span>
                </div>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="drafts.length" :page-size="5" layout="prev, pager, next"
              @current-change="changeDraftPage">
            </el-pagination>
          </div>
          <el-empty v-else description="这里什么也没有" style="margin-top:80px;"></el-empty>
        </el-tab-pane>
        <el-tab-pane label="待审核" v-if="isSelf">
          <div class="publish-list" v-if="audits.length > 0">
            <div class="publish-list-item" v-for="audit in audits.slice((auditPage - 1) * 5, auditPage * 5)"
              :key="audit.id" @click="goAudit(audit.id)">
              <div class="publish-list-item-cover">
                <img :src="audit.cover" alt="图片找不到了QAQ">
              </div>
              <div class="publish-list-item-text">
                <h1 class="publish-list-item-title">
                  {{ audit.title }}
                </h1>
                <div>
                  <span class="publish-list-item-description">
                    {{ audit.description }}
                  </span>
                </div>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="audits.length" :page-size="5" layout="prev, pager, next"
              @current-change="changeAuditPage">
            </el-pagination>
          </div>
          <el-empty v-else description="这里什么也没有" style="margin-top:80px;"></el-empty>
        </el-tab-pane>
        <el-tab-pane label="已退回" v-if="isSelf">
          <div class="publish-list" v-if="backs.length > 0">
            <div class="publish-list-item" v-for="back in backs.slice((backPage - 1) * 5, backPage * 5)" :key="back.id"
              @click="goBack(back.id)">
              <div class="publish-list-item-cover">
                <img :src="back.cover" alt="图片找不到了QAQ">
              </div>
              <div class="publish-list-item-text">
                <h1 class="publish-list-item-title">
                  {{ back.title }}
                </h1>
                <div>
                  <span class="publish-list-item-description">
                    {{ back.description }}
                  </span>
                </div>
              </div>
            </div>
            <el-pagination :hide-on-single-page="false" :total="backs.length" :page-size="5" layout="prev, pager, next"
              @current-change="changeBackPage">
            </el-pagination>
          </div>
          <el-empty v-else description="这里什么也没有" style="margin-top:80px;"></el-empty>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import request from '@/API/request';
import { Message } from 'element-ui';
export default {
  name: 'PublishInfo',
  data() {
    return {
      publishPage: 1,
      draftPage: 1,
      auditPage: 1,
      backPage: 1,
      publishes: [],
      drafts: [],
      audits: [],
      backs: [],
      loading: true,
    }
  },
  computed: {
    isSelf() {
      return this.$route.query.id === this.$store.state.userInfo.id;
    }
  },
  methods: {
    goPublish(id) {
      this.$router.push({ path: '/route', query: { id } });
    },
    goDraft(id) {
      this.$router.push({
        path: '/editor', query: {
          id,
          new: '0'
        }
      });
    },
    goAudit(id) {
      this.$router.push({ path: '/route', query: { id } });
    },
    goBack(id) {
      request.get('/route/rewrite', {
        params: {
          userId: this.$store.state.userInfo.id,
          routeId: id
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.$router.push({
            path: '/editor', query: {
              id,
              new: '0'
            }
          });
        } else {
          Message({
            type: 'error',
            message: res.data.msg,
            customClass: 'message-global'
          })
        }
      }).catch(err => {
        Message({
          type: 'error',
          message: err,
          customClass: 'message-global'
        })
      })
    },
    changePublishPage(page) {
      this.publishPage = page;
    },
    changeDraftPage(page) {
      this.draftPage = page;
    },
    changeAuditPage(page) {
      this.auditPage = page;
    },
    changeBackPage(page) {
      this.backPage = page;
    },
  },
  mounted() {
    request.get('/route/user', {
      params: {
        id: this.$route.query.id,
      }
    }).then(res => {
      if (res.data.code === 200) {
        this.publishes = res.data.data
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch(err => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/route/user/drafts', {
      params: {
        id: this.$route.query.id,
      }
    }).then(res => {
      if (res.data.code === 200) {
        this.drafts = res.data.data
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch(err => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/route/user/audit', {
      params: {
        id: this.$route.query.id,
      }
    }).then(res => {
      if (res.data.code === 200) {
        this.audits = res.data.data
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch(err => {
      Message.error(err);
    });
    request.get('/route/user/return', {
      params: {
        id: this.$route.query.id,
      }
    }).then(res => {
      if (res.data.code === 200) {
        this.backs = res.data.data
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch(err => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    this.loading = false;
  }
}
</script>

<style scoped>
.main {
  width: 100%;
  min-height: 520px;
  background-color: #fff;
  border-radius: 5px;
}

.publish {
  text-align: left;
  margin: 5px;
}

.publish-list {
  min-height: 510px;
}

.publish-list-item {
  border: solid 1px rgba(207, 205, 205, 0.842);
  padding: 3px;
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 4px;
  transition: all 0.5s ease;
}

.publish-list-item:hover {
  border: solid 1px #409eff;
  box-shadow: 0 0 10px #409eff;
  cursor: pointer;
}

.publish-list-item-cover {
  display: inline-block;
  border-radius: 4px;
  position: relative;
  top: 0px;
  border-radius: 4px;
}

img {
  width: 200px;
  height: 140px;
  border-radius: 4px;
  object-fit: cover;
}

.publish-list-item-text {
  display: inline-block;
  margin-left: 10px;
  position: relative;
  text-align: left;
  vertical-align: top;
  height: 100px;
  top: 15px;
  width: 60%;
}

.publish-list-item-title {
  position: relative;
  top: 0px;
  display: inline-block;
  line-height: 35px;
  margin-left: 10px;
  margin-bottom: 0px;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.publish-list-item-author {
  position: relative;
  display: inline-block;
  font-size: 13px;
  margin-left: 10px;
  margin-top: 10px;
  top: 30px;
  border-radius: 1px;
  transition: all 0.3s ease;
}

.publish-list-item-author:hover {
  box-shadow: #235588 0px 0px 0px 1px;
  color: #235588;
  cursor: pointer;
}

.publish-list-item-description {
  display: inline-block;
  font-size: 13px;
  margin-left: 10px;
  margin-top: 10px;
  color: rgb(134, 134, 134);
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.el-pagination {
  text-align: center;
}
</style>